/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ressources;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import modele.Database;
import modele.DatabaseManager;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

/**
 *
 * @author Roman
 */
public class TableRessource extends ServerResource {
    private String dbName;
    private String tbName;
    Representation resultat;

    protected void doInit() throws ResourceException {
        // Get the "itemName" attribute value taken from the URI template
        // /items/{itemName}.
        this.dbName = (String) getRequest().getAttributes().get("dbName");
        this.tbName = (String) getRequest().getAttributes().get("tableName");

        Database.setConnectionData(dbName, "root", "");
        // Get the item directly from the "persistence layer".
        this.resultat = DatabaseManager.getRows(tbName);
        setExisting(this.resultat != null);
    }

    /** Représentation en réponse à une requête de méthode GET */
    @Get("text/xml")
    public Representation toXML() {
        System.out.println("GET Table");
        return resultat;
    }

        /** Représentation en réponse à une requête de méthode POST */
    @Post("text/xml")
    public void addRow(Representation entity) {
        System.out.println("post row");
        if (getStatus().isSuccess()) {
            Document domParam;
            try {
                domParam = new DomRepresentation(entity).getDocument();
                NodeList fields = domParam.getElementsByTagName("field");
                String lstColumns="";
                String lstValues="";
                for(int i = 0; i<fields.getLength() ; ++i){
                    if(i!=0){
                        lstColumns+= ", ";
                        lstValues+= ", ";
                    }
                    NamedNodeMap nnm = fields.item(i).getAttributes();
                    String colonne = nnm.getNamedItem("name").getNodeValue();
                    String value = fields.item(i).getFirstChild().getNodeValue();
                    lstColumns+= colonne;
                    lstValues+= "\"" + value +"\"";
                }

                String sql = "insert into " + tbName + " (" + lstColumns + ") values (" + lstValues + ")";
                System.out.println(sql);
                Database db = Database.getInstance();
                db.write(sql);

            } catch (IOException ex) {
                Logger.getLogger(LigneRessource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}